package com.zhou;

import com.zhou.spider.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        Main main = new Main();

        while (true) {
            System.out.printf("");
            System.out.println("=============================");
            System.out.println("请输入数字选择要进行的操作：");
            System.out.println("1.爬取压缩文件列表");
            System.out.println("2.下载压缩文件");
            System.out.println("3.提取专利号");
            System.out.println("4.爬取专利数据");
            System.out.println("0.退出");
            System.out.printf("==============================");
            System.out.println();

            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();
            if (num == 0) {
                return;
            } else if (num == 1) {
                try {
                    main.testDownloadFileName();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (num == 2) {
                Map<Integer, Integer> year2num = main.getYear2num();
                ArrayList<Integer> years = new ArrayList<>(year2num.keySet());
                years.sort((y1, y2) -> (y1 - y2));

                System.out.println("年份\t个数");
                for (int i = 0; i < years.size(); i++) {
                    int year = years.get(i);
                    int n = year2num.get(year);
                    System.out.println(String.format("%d\t%d", year, n));
                }

                while (true) {
                    System.out.println("请输入要下载的年份: ");
                    int year = scanner.nextInt();
                    if(year == 0){
                        return;
                    } else if (year2num.containsKey(year) == false) {
                        System.out.println("输入年份无数据，请重新输入!");
                        continue;
                    } else {
                        main.downloadByYear(year);
                    }
                }
            } else {
                System.out.println("您输入的不合法，请重新输入!");
            }
        }
    }
}
